package com.csaba.swing.gui.bha;

import java.awt.Component;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;

import com.csaba.connector.bha.BHALoginService;
import com.csaba.connector.model.Customer;
import com.csaba.swing.gui.login.AbstractLoginPage;
import com.csaba.swing.gui.login.LoginDetails;
import com.csaba.swing.gui.login.LoginWizardModel;
import com.csaba.swing.gui.util.MaxLengthDocument;
import com.csaba.swing.gui.util.RegExpDocument;
import com.csaba.swing.gui.wizard.WizardContext;
import com.csaba.swing.gui.wizard.WizardDialog;
import com.csaba.util.I18N;

@SuppressWarnings( "serial" )
public class BHALoginPage extends AbstractLoginPage
{
	private static final String LD_PASSWORD = "password"; //$NON-NLS-1$

	private JTextField loginIdField;
	private JPasswordField passwordField;
	private JCheckBox savePassword;

	@Override
	public void init( final WizardContext context )
	{
		super.init(context);

		add(new JLabel(I18N.getString(this, "BHALoginPage.loginId"))); //$NON-NLS-1$
		add(loginIdField = new JTextField(new RegExpDocument("[0-9a-z]*", BHALoginService.TOKEN_LOGINID_LENGTH), "", //$NON-NLS-1$ //$NON-NLS-2$
				BHALoginService.TOKEN_LOGINID_LENGTH));
		add(new JLabel(I18N.getString(this, "BHALoginPage.password"))); //$NON-NLS-1$
		add(passwordField = new JPasswordField(new MaxLengthDocument(BHALoginService.PASSWORD_MAX_LENGTH),
				"", BHALoginService.PASSWORD_MAX_LENGTH)); //$NON-NLS-1$
		add(new JSeparator());
		add(savePassword = new JCheckBox(I18N.getString(this, "BHALoginPage.savePassword"))); //$NON-NLS-1$
		savePassword.setEnabled(false);

		addChangeListener(loginIdField);
		addChangeListener(passwordField);
	}

	@Override
	public boolean leave( final int action )
	{
		if ( action == ACTION_FINISH )
		{
			final Customer customer = new Customer();
			customer.setLoginId(loginIdField.getText());
			customer.setPassword(new String(passwordField.getPassword()));
			return doLogin(customer);
		}

		return true;
	}

	@Override
	protected void updateMessage( final Component component )
	{
		super.updateMessage(component);

		final WizardDialog dialog = context.getDialog();
		boolean ready = true;
		boolean passwordBased = false;

		final String loginId = loginIdField.getText();

		final int authType = BHALoginService.detectAuthType(loginId);

		switch ( authType )
		{
		case BHALoginService.AUTH_TYPE_PASSWORD:
			passwordBased = true;
			if ( passwordField.getPassword().length < 1 )
			{
				dialog.setWarning(I18N.getString(this, "BHALoginPage.msg.validPassword")); //$NON-NLS-1$
				ready = false;
			}
			break;

		case BHALoginService.AUTH_TYPE_TOKEN:
			if ( passwordField.getPassword().length != BHALoginService.TOKEN_LENGTH )
			{
				dialog.setWarning(I18N.getString(this, "BHALoginPage.msg.validToken")); //$NON-NLS-1$
				ready = false;
			}
			break;

		default:
			dialog.setWarning(I18N.getString(this, "BHALoginPage.msg.validLoginId")); //$NON-NLS-1$
			ready = false;
			break;
		}

		savePassword.setEnabled(passwordBased && ( (LoginWizardModel) context.getWizardModel() ).isSaveDetails());
		if ( !savePassword.isEnabled() )
			savePassword.setSelected(false);

		if ( ready )
		{
			dialog.clearMessage();
		}

		dialog.setReady(ready);
	}

	@Override
	protected LoginDetails getDetailsToSave( final Customer customer )
	{
		final LoginDetails details = new LoginDetails(customer.getBank().getName(), customer.getLoginId());

		if ( savePassword.isSelected() )
			details.put(LD_PASSWORD, customer.getPassword());

		return details;
	}

	@Override
	protected void setSavedDetails( final LoginDetails details )
	{
		loginIdField.setText(details.getLoginId());
		final String password = details.get(LD_PASSWORD);
		passwordField.setText(password);
		savePassword.setSelected(password != null);
	}

}
